﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace PropertyBuilder
{
    public partial class PropertyBuilderForm : Form
    {
        string _builtProperties;

        public PropertyBuilderForm()
        {
            InitializeComponent();   
        }

        private void btnBuild_Click(object sender, EventArgs e)
        {
            var tempList= txtIngredients.Text.Split(';').ToList();
            var trimmedList= tempList.Select(text => text.Replace("\r\n", "")).ToList();
            try
            {
                 _builtProperties = Output(trimmedList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n" +
                                "Start all over.\r\nHave space between type and name and use semicolon after name.");
            }          
            txtBuiltProperties.Text = _builtProperties;
        }

        private void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            txtBuiltProperties.SelectAll();
            Clipboard.SetText(txtBuiltProperties.SelectedText);            
        }


        public  string Output(List<string> list)
        {
            string type = String.Empty;
            string name = String.Empty;
            string privateName = String.Empty;
            string publicName = String.Empty;
            StringBuilder sb1=new StringBuilder();
            StringBuilder sb2=new StringBuilder();
                
            foreach (string ss in list)
            {
                if (ss.Length<3)
                {
                    break;
                }
                string[] aa = ss.Split(' ');
                type = aa[0];
                name = aa[1];
                privateName = "_" + name;
                string firstLetter = name.Substring(0, 1);

                publicName = firstLetter.ToUpper() + name.Remove(0, 1);
                sb1.Append("        private " + type + " " + privateName+";\r\n");
                sb2.Append("        public " + type + " " + publicName + "\r\n");
                sb2.Append("        {\r\n");
                sb2.Append("            get \r\n            {\r\n");
                sb2.Append("                return "+privateName+";\r\n");
                sb2.Append("            }\r\n");
                sb2.Append("            set\r\n");
                sb2.Append("            {\r\n");
                sb2.Append("                if(" + privateName + "!=value)\r\n");
                sb2.Append("                {\r\n");
                sb2.Append("                    " + privateName + "=value;\r\n");
                sb2.Append("                    OnPropertyChanged(\"" + publicName + "\");\r\n");
                sb2.Append("                }\r\n");
                sb2.Append("            }\r\n");
                sb2.Append("        }\r\n\r\n");

            }
            return ("\r\n"+sb1.ToString() + sb2.ToString());           
        }    
    }
}
    
